package com.baizhi.service.impl;

import com.baizhi.dao.BzAdminMapper;
import com.baizhi.entity.BzAdmin;
import com.baizhi.service.BzAdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class BzAdminServiceImpl implements BzAdminService {

    @Resource
    private BzAdminMapper  bzAdminMapper;

    public List<BzAdmin> selectAll() {
        return bzAdminMapper.selectList(null);
    }


    @Override
    public boolean login(String name, String password) {
        boolean flag=true;
        try {
            QueryWrapper<BzAdmin> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",name);

            BzAdmin bzAdmin = bzAdminMapper.selectOne(queryWrapper);

            if(bzAdmin== null) {
                flag = false;
            }else if(!bzAdmin.getPassword().equals(password)) {
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;

    }

    @Override
    public Page<BzAdmin> selectAll(Integer page, Integer limit) {
        /**
         * 查询 第一页 数据 每页两条数据  page 1  limit 2（对应的Controller参数）
         *
         * 创建IPage对象 封装分页条件
         *
         * 但是MybatisPlus不需要任何的计算 直接将Controller收到参数赋值给Page对象即可
         */
        Page<BzAdmin> bzAdminPage = new Page<>(page,limit);


        /**
         * IPage对象  封装分页条件 第几页（页码 page） 每页多少行数据（limit）
         *
         * 条件构造器  封装 where 条件 如果没有写null
         *
         * 返回值page对象
         */

        Page<BzAdmin> adminPage = bzAdminMapper.selectPage(bzAdminPage,null);

        /**
         * 3.从page对象中 拿到分页的结果
         *
         * getRecords() 获取分页结果的数据
         * getTotal() 总条数
           ,limit:10
          ,limits:[5,10]
         */
        return adminPage;
    }

    @Override
    public void insert(BzAdmin bzAdmin) {

        try {
            bzAdminMapper.insert(bzAdmin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public BzAdmin showOne(Integer id) {

        return bzAdminMapper.selectById(id);
    }

    @Override
    public void delete(Integer id) {
        try {
            bzAdminMapper.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(BzAdmin bzAdmin) {
        try {
            bzAdminMapper.updateById(bzAdmin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void deleteAll(List<BzAdmin> ids) {
        try {
            bzAdminMapper.deleteBatchIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
